<?php
include("../../tools/session.php");
include("../../tools/controls.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
$page_status = $_POST["page_status"];
// $page_status = $_GET["page_status"];

// 从 session 中获取的用户 ID 号
$session_id = $_SESSION["user"]["id"];
$user_id = $session_id;

$per_page = 10;

include("../../tools/mysql.php");
db_connect();

// 如果没有传入的参数，则显示页面
if (!$page_status)
{
   include("tpl-inc-wp.php");
   
   // $sql = "SELECT ceil(count(to_id)/" . $per_page . ") num FROM document WHERE to_id=" . $user_id . " AND is_finished=0";
   // $sql = "SELECT ceil(count(a.handout_id)/" . $per_page . ") num FROM doc_exam a LEFT JOIN document b ON a.doc_id=b.doc_id WHERE a.handout_id=" . $user_id . " AND a.has_examed=0 AND b.is_finished=0";
   $sql = "SELECT count(a.handout_id) num FROM doc_exam a LEFT JOIN document b ON a.doc_id=b.doc_id WHERE a.handout_id=" . $user_id . " AND a.has_examed=0 AND b.is_finished=0";
   $re = db_query_once($sql);
   
   $tpl->assign("user_id", $user_id);
   // $tpl->assign("page_num", $re["num"]);
   $tpl->assign("wholepage", $re["num"]);
   $tpl->display($oa_tpl_path . "/default/wait_for_examining.html");
}
// 如果传入的参数为 1，则显示公文记录数据 XML
elseif ($page_status == 1)
{
   header("content-type: text/xml");
   echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
   echo "<document>\n";
   
   $page = $_POST["page"];
   $sql = "SELECT DISTINCT a.zh_name, a.name, b.doc_id, c.username, b.title, b.instancy,
			   DATE_FORMAT(b.apply_time, '%Y-%m-%d') apply_time, b.is_multi, b.is_sent,
			   b.is_canceled, b.is_finished, b.is_passed_at_last
		   FROM view_doc_tpl a, document b, user c, doc_exam d
		   WHERE a.tpl_id=b.tpl_id
			   AND b.doc_id=d.doc_id
			   AND b.from_id=c.id
			   AND d.handout_id=" . $user_id . "
			   AND d.has_examed=0
			   AND b.is_canceled=0
			   AND b.is_finished=0
			 ORDER BY b.apply_time DESC ";
   $re = db_query_page($sql, $page, $per_page);
   
   $str = "";
   foreach($re as $v)
	 {
		$str .= "<item>\n";
		$str .= "<doc_id>" . $v["doc_id"] . "</doc_id>\n";
		$str .= "<class_name>" . $v["zh_name"] . "</class_name>\n";
		$str .= "<tpl_name>" . $v["name"] . "</tpl_name>\n";
		$str .= "<username>" . $v["username"] . "</username>\n";
		$str .= "<title><![CDATA[" . $v["title"] . "]]></title>\n";
		$str .= "<instancy>" . $v["instancy"] . "</instancy>\n";
		$str .= "<apply_time><![CDATA[" . $v["apply_time"] . "]]></apply_time>\n";
		$str .= "<is_multi>" . $v["is_multi"] . "</is_multi>\n";
		$str .= "<is_sent>" . $v["is_sent"] . "</is_sent>\n";
		$str .= "<is_canceled>" . $v["is_canceled"] . "</is_canceled>\n";
		$str .= "<is_finished>" . $v["is_finished"] . "</is_finished>\n";	
		$str .= "<is_passed>" . $v["is_passed"] . "</is_passed>\n";
		$str .= "</item>\n";
	 }
   echo $str;
   
   echo "</document>\n";
}

?>

